var base_url = "http://www.ibomobi.com/demo/symbian/wrt/"

function ajaxUpdater(id, path) {
  var this_url = base_url + path;
  new Ajax.Updater(id, this_url, {asynchronous:true});
}

function sayHello(id, path){
  var this_url = base_url + path;
  var pars = 'hi-name='+escape($F('hi-name'));
  var myAjax = new Ajax.Updater(id, this_url, {method: 'get', parameters: pars});
}


function sendAjaxLogin(form, id)
{
  var this_url = base_url + "ajax/ajax_login.php";
  var pars = $(form).serialize();
  alert(pars);  // show parameters
  var myAjax = new Ajax.Updater(id, this_url, {method: 'get', parameters: pars});
}

Object.extend(String.prototype, {
  mixin: function(obj) {
    return new Template(this).evaluate(obj);
  }
});

function test_json()
{
  var this_url = base_url + "ajax/test_json.php";
  // show progress animation gif
  $('waitpic').style.display = 'block';
  new Ajax.Request(this_url,   
    {
	  method:'get',     
	  onSuccess: getResponse, 
	  onFailure: function(){ 
	    // hide progress animation gif
        $('waitpic').style.display = 'none';
	    alert('Something went wrong...');
	  }   
    }); 
}

function getResponse(transport, json)
{ 
  var data = transport.responseText.evalJSON(true);  
//  var data = eval(transport.responseText);
//  alert(data);

  // show raw json data.
//  $('tab4_result').innerHTML = transport.responseText; 

  // clear result area
  $('tab4_result').innerHTML = ''; 
  
  // build table body
  var templ = new Template($('row_template').value);  // not $('row_template').innerHTML
  var bodyFormatted = "";
  //populate the list
  for (var i = 0; i < data.length; i++) {
    // 
    var item = data[i][0];
//	alert(item.product);
    bodyFormatted += templ.evaluate(item) + '\n';
  }
  // build table header
  var item0 = data[0][0];
  var keys = Object.keys(item0);
  templ = new Template($('headcell_template').value);
  var headFormatted = "";
  for (var i=0; i<keys.length; i++) {
    headFormatted += templ.evaluate({"hname": keys[i]});
  }
  // assembly table.  
  var tableTemplateString = $('table_template').value;
  var pars = {"heads": headFormatted, "rows": bodyFormatted};  
  $('tab4_result').innerHTML = tableTemplateString.mixin(pars);
  
  // hide progress animation gif
  $('waitpic').style.display = 'none';
}
